/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2023. All rights reserved.
 *
 * Function : z = x + y
 * This sample is a very basic sample that implements vector add on Ascend plaform.
 */
#include "kernel_operator.h"
// tensor num for each queue
constexpr int32_t BUFFER_NUM = 1;
constexpr int32_t ITERATION_COUNT = 55;
constexpr int32_t BUFFER_SIZE = 2048;

template<typename TYPE_X, 
         typename TYPE_Y, 
         typename TYPE_Z> 
class KernelAdd {
    using T = TYPE_X;
public:
    __aicore__ inline KernelAdd() {}
    __aicore__ inline void Init(GM_ADDR x, GM_ADDR y, GM_ADDR z, uint32_t titlenumber,
                                uint32_t bigCoreNum) 
    {
        ASSERT(AscendC::GetBlockNum() != 0 && "block dim can not be zero!");
        this->processDataNum=BUFFER_SIZE;
        uint32_t coreNum = AscendC::GetBlockIdx();
        uint32_t globalBufferIndex = coreNum*(titlenumber+1)*BUFFER_SIZE;
        
        this->tileNum=titlenumber+1;
        
        if (coreNum >= bigCoreNum) { 
          this->tileNum=titlenumber;
         
          globalBufferIndex = (bigCoreNum+coreNum*titlenumber)*BUFFER_SIZE;
        }
       
        xGm.SetGlobalBuffer((__gm__ TYPE_X*)x + globalBufferIndex, (titlenumber+1)*BUFFER_SIZE);
        yGm.SetGlobalBuffer((__gm__ TYPE_Y*)y + globalBufferIndex, (titlenumber+1)*BUFFER_SIZE);
        zGm.SetGlobalBuffer((__gm__ TYPE_Z*)z + globalBufferIndex, (titlenumber+1)*BUFFER_SIZE);
        pipe.InitBuffer(inQueueX, BUFFER_NUM, this->processDataNum * sizeof(TYPE_X));
        pipe.InitBuffer(inQueueY, BUFFER_NUM, this->processDataNum * sizeof(TYPE_Y));
        pipe.InitBuffer(outQueueZ, BUFFER_NUM, this->processDataNum * sizeof(TYPE_Z));
    }
    __aicore__ inline void Process()
    {
        int32_t loopCount = this->tileNum;
        for (int32_t i = 0; i < loopCount; i++) {
            CopyIn(i);
            Compute(i);
            CopyOut(i);
        }
    }

private:
    __aicore__ inline void CopyIn(const int32_t progress)
    {
      AscendC::LocalTensor<TYPE_X> xLocal = inQueueX.AllocTensor<TYPE_X>();
      AscendC::LocalTensor<TYPE_Y> yLocal = inQueueY.AllocTensor<TYPE_Y>();
      AscendC::DataCopy(xLocal, xGm[progress * this->processDataNum], this->processDataNum);
      AscendC::DataCopy(yLocal, yGm[progress * this->processDataNum], this->processDataNum);
      inQueueX.EnQue(xLocal);
      inQueueY.EnQue(yLocal);
    }
    __aicore__ inline void Compute(const int32_t progress)
    {
      AscendC::LocalTensor<TYPE_X> xLocal = inQueueX.DeQue<TYPE_X>();
      AscendC::LocalTensor<TYPE_Y> yLocal = inQueueY.DeQue<TYPE_Y>();
      AscendC::LocalTensor<TYPE_Z> zLocal = outQueueZ.AllocTensor<TYPE_Z>();
      
      for(int32_t start=0;start<ITERATION_COUNT;start++){
        AscendC::Muls(xLocal,xLocal,(TYPE_X)1.0,this->processDataNum);
        AscendC::Add(xLocal,xLocal,yLocal,this->processDataNum);}
      AscendC::Muls(zLocal,xLocal,(TYPE_X)1.0,this->processDataNum);

      outQueueZ.EnQue<TYPE_Z>(zLocal);
      inQueueX.FreeTensor(xLocal);
      inQueueY.FreeTensor(yLocal);
    }
    __aicore__ inline void CopyOut(const int32_t progress)
    {
      AscendC::LocalTensor<TYPE_Z> zLocal = outQueueZ.DeQue<TYPE_Z>();  
      AscendC::DataCopy(zGm[progress * this->processDataNum], zLocal, this->processDataNum);
      outQueueZ.FreeTensor(zLocal);
    }

private:
    AscendC::TPipe pipe;
    AscendC::TQue<AscendC::QuePosition::VECIN, BUFFER_NUM> inQueueX;
    AscendC::TQue<AscendC::QuePosition::VECIN, BUFFER_NUM> inQueueY;
    AscendC::TQue<AscendC::QuePosition::VECOUT, BUFFER_NUM> outQueueZ;
    AscendC::GlobalTensor<TYPE_X> xGm;
    AscendC::GlobalTensor<TYPE_Y> yGm;
    AscendC::GlobalTensor<TYPE_Z> zGm;
    uint32_t tileNum;
    uint32_t processDataNum;
};

extern "C" __global__ __aicore__ void add_custom(GM_ADDR x, 
                                                 GM_ADDR y,
                                                 GM_ADDR z, 
                                                 GM_ADDR workspace, 
                                                 GM_ADDR tiling)
{
    GET_TILING_DATA(tiling_data, tiling);
    KernelAdd<DTYPE_X, DTYPE_Y, DTYPE_Z> op;
    op.Init(x, y, z, tiling_data.titlenumber, 
            tiling_data.bigCoreNum);  
    op.Process();
}